﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TotalNews.Models;
using TotalNews.Helpers;

namespace TotalNews.Controllers
{
    public class ItemController : Controller
    {
        ItemRepository item=new ItemRepository();
        //
        // GET: /Item/
        public ActionResult Index()
        {
            ViewData["topnews"] = item.GetTopNewsAfterOne();
            ViewData["onenews"] = item.GetOneItem();
            ViewData["r_docnhieunhat"] = item.GetTindocnhieu();
            return View();
        }
        public ActionResult News(int id,int page=0)
        {
            const int pagesize = 10;
            IQueryable<TotalNews.Models.tbl_Item> i=null;
            tbl_Category c = null;
            ViewData["singleitem"] = item.GetNewItemByCateId(id);
           // ViewData["rightdetails"]=item.get
            c= item.GetSingleCategoriesById(id);
            if (c.ParentID != null)
            {
                ViewData["catId"] = c.ParentID;
            }
            else
            {
                ViewData["catId"] = c.ID;
            }
            //ViewData["allitem"] = item.GetItemFromCategory(id);
            i=item.GetItemFromCategory(id);
            //if (ViewData["singleitem"] == null || ViewData["allitem"] == null)
            //    return View("Error");
            var paginnatedItem = new PaginatedList<TotalNews.Models.tbl_Item>(i,page, pagesize);
            return View(paginnatedItem);
        }
        public ActionResult Details(int id)
        {
            tbl_Item it = item.GetItemById(id);
            if (it.tbl_Category.ParentID != null)
            {
                ViewData["catId"] = it.tbl_Category.ParentID;
            }
            else
            {
                ViewData["catId"] = it.CategoryID;
            }
            if (it == null)
                return View("Error");
            return View(it);
        }
    }
}
